//
//  gouwuchelistViewController.m
//  BAOASystem
//
//  Created by michael on 2018/3/8.
//  Copyright © 2018年 czd. All rights reserved.
//

#import "gouwuchelistViewController.h"
#import "shangpinxqViewController.h"
@interface gouwuchelistViewController ()

@end

@implementation gouwuchelistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"购物车",nil);
    self.view.backgroundColor = [UIColor whiteColor];
      _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight-kTopHeight) style:UITableViewStylePlain];
      _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.delegate = self;
[self.view addSubview:_tableView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

       __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{

        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
   if (weakSelf.lastPage==0 ){

     [weakSelf loadMore];

        }else{
            
            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:NSLocalizedString(@"没有更多了", nil)];
            
        }
        
    }];

    // Do any additional setup after loading the view.
}
- (void)loadData{
_currentPage = 1;
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
      NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"];
      NSString *cpid = [[NSUserDefaults standardUserDefaults] objectForKey:@"cpid"];

    NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><uid>%@</uid><cpid>%@</cpid><pageNumber>%d</pageNumber><pageSize>20</pageSize></body></message>",userName,password,website,language,BAtype,uid,cpid,_currentPage];
     bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bCart];
     [HttpTools requestWithURL:path arrays:array method:@"cartList" success:^(id JSON) {
         [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        NSDictionary *dic = JSON[@"cartpage"];
   NSLog(@"%@",JSON);

          _lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
      NSArray *array = dic[@"list"];
 if (array.count==0)
        {
             [self.view makeToast:NSLocalizedString(@"没有数据", nil)];
        }
                     _dataArray =   [NSMutableArray array];
 for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    }
[_tableView reloadData];
            [self.tableView headerEndRefreshing];
        self.currentPage++;


 } failure:^(NSError *error) {
             [self.tableView headerEndRefreshing];

    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

    }];

    

}
- (void)loadMore{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
      NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"];
      NSString *cpid = [[NSUserDefaults standardUserDefaults] objectForKey:@"cpid"];

    NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><uid>%@</uid><cpid>%@</cpid><pageNumber>%d</pageNumber><pageSize>20</pageSize>/body></message>",userName,password,website,language,BAtype,uid,cpid,_currentPage];
     bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bCart];
     [HttpTools requestWithURL:path arrays:array method:@"cartList" success:^(id JSON) {
   NSLog(@"%@",JSON);
        NSDictionary *dic = JSON[@"cartpage"];
NSArray *array = dic[@"list"];
  _lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
   for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
}
[_tableView reloadData];
        [self.tableView footerEndRefreshing];

        self.currentPage++;

} failure:^(NSError *error) {

    }];

    

}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"getProductLis";

  gouwuTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

if (cell==nil) {
        cell=[[gouwuTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                      cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    return cell;



 }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 
    return  10+100+30+10;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"删除", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了删除");
        NSDictionary *dic = _dataArray[indexPath.row];
        [self scaction:dic[@"id"]];
        }];
      deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"ff5a2d"];
      return @[deleteRowAction];
      }
- (void)scaction:(NSString *)idstring{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><id>%@</id></body></message>",userName,password,website,language,BAtype,idstring];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bCart];
 [HttpTools requestWithURL:path arrays:array method:@"deleteCart" success:^(id JSON) {
   NSLog(@"%@",JSON);

    [self.view makeToast:JSON[@"msg"]];
//[_tableView reloadData];
[self loadData];
 } failure:^(NSError *error) {

    }];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSDictionary * dic = self.dataArray[indexPath.row];
        NSDictionary *dict = dic[@"product"];

shangpinxqViewController *vc = [[shangpinxqViewController alloc]init];
vc.pdid = dict[@"id"];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
